//
//  LRXLoginViewController.m
//  路人侠
//
//  Created by zhangxulong on 2/17/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXLoginViewController.h"
#import "zxlCircleView.h"
#import "LRXLoginView.h"
#import "TWTSideMenuViewController.h"
#import "LRXRegisterController.h"
#import "LRXNavigationController.h"
#import "AppDelegate.h"
#import "LRXRealNameController.h"
@interface LRXLoginViewController() <
    LRXLoginViewDelegate

>

@property (nonatomic,weak) UIView * titleView;

@property (nonatomic,weak) LRXLoginView * loginView;

@end
@implementation LRXLoginViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willHide) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad
{
    self.view.backgroundColor = [UIColor colorWithRed:233 green:241 blue:253];
    
    //标题的设置
    self.navigationItem.title = @"登录";
    //添加 头像 布局
    UIImageView * loginImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"loginBackground"]];
    loginImage.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:loginImage];
    
    //布局 登陆界面
    [self addLoginView];
    
    //忘记密码 样式布局
    [self layoutOtherView];
    

}


//忘记密码布局
- (void)layoutOtherView
{
    UIButton * forgetBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [forgetBtn setTitle:@"忘记密码？" forState:UIControlStateNormal];
    [forgetBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    forgetBtn.titleLabel.font = [UIFont systemFontOfSize:20 * hScale];
    [forgetBtn sizeToFit];
    
    CGFloat otherViewCenterY = CGRectGetMaxY(self.loginView.frame) + 30 * hScale;
    CGFloat otherViewCenterX = self.loginView.center.x;
    forgetBtn.center = CGPointMake(otherViewCenterX, otherViewCenterY);
    
    CGFloat regBtnY = CGRectGetMaxY(forgetBtn.frame) + 30 * hScale;
    CGFloat remainLabelX = 208 * wScale;
    CGFloat regBtnX = 380 * wScale;
    
    UILabel * remainLabel  = [[UILabel alloc]init];
    remainLabel.text = @"还没有帐号？";
    remainLabel.textColor = [UIColor whiteColor];
    remainLabel.font = [UIFont systemFontOfSize:24 * hScale];
    remainLabel.frame = CGRectMake(remainLabelX, regBtnY, 0, 0);
    [remainLabel sizeToFit];
    
    UIButton * regBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [regBtn setTitle:@"注册" forState:UIControlStateNormal];
    [regBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    regBtn.titleLabel.font = remainLabel.font;

    
    regBtn.frame = CGRectMake(regBtnX, regBtnY - 8 * hScale,0, 0);
    [regBtn sizeToFit];
    
    [regBtn addTarget:self action:@selector(pushRegViewController) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:remainLabel];
    [self.view addSubview:regBtn];
    [self.view addSubview:forgetBtn];
    
}

//登陆主页面布局
- (void)addLoginView
{
    LRXLoginView * loginView = [LRXLoginView layoutLoginViewSubviews];
    loginView.remindDelegate = self;
    loginView.layer.cornerRadius = 10 * hScale;
    CGFloat loginViewY = 420 * hScale;
    CGFloat loginViewW = MainScreenW * 0.7;
    CGFloat loginViewX = (MainScreenW - loginViewW) / 2;
    
    loginView.frame = CGRectMake(loginViewX,loginViewY,loginViewW, 340 * hScale);
    
    self.loginView = loginView;
    [self.view addSubview:loginView];
}



#pragma mark - lrxLogView 的代理方法
- (void)loginSuccess
{
    NSUserDefaults *defualts = [NSUserDefaults standardUserDefaults];
    NSString * firstSelectSex = [defualts objectForKey:@"firstSelectSex"];
    if ([firstSelectSex isEqualToString:@"1"]) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    else{
        
        [defualts setObject:@"1" forKey:@"firstSelectSex"];
        [defualts synchronize];
    
        [self presentViewController:[LRXRealNameController new] animated:YES completion:^{
            [self.navigationController popToRootViewControllerAnimated:YES];
        }];
    }
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    
}

//推出注册界面
- (void)pushRegViewController
{
    LRXRegisterController * regController = [[LRXRegisterController alloc]init];
    LRXNavigationController * navController = [[LRXNavigationController alloc]initWithRootViewController:regController];
    
    [self presentViewController:navController animated:YES completion:nil];
}

- (void)willShow:(NSNotification *)noti
{
    UITextField * currentTf = self.loginView.textFiels[[self indexOfFirstResponder]];
    CGPoint point = CGPointMake(CGRectGetMinX(currentTf.frame), CGRectGetMaxY(currentTf.frame));
    
    CGFloat maxY = [currentTf convertPoint:point toView:self.view].y;
    
    //2.获取键盘的y值
    CGRect kbEndFrm = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat kbY = kbEndFrm.origin.y - 80 * hScale;
    
    //3.进行比较  计算距离
    CGFloat delta = kbY - maxY;
    //需要往上移
    if(delta < 0){
        [UIView animateWithDuration:0.25 animations:^{
            self.view.transform = CGAffineTransformMakeTranslation(0, delta);
        }];
    }
}
- (void)willHide
{
    /**
     * 恢复原状
     */
    [UIView animateWithDuration:0.25 animations:^{
        self.view.transform = CGAffineTransformIdentity;
    }];
}


/**
 *  获取当前的响应者的索引
 */
-(NSInteger)indexOfFirstResponder{
    
    //isFirstResponder 判断对象是否时第一响应者
    
    for (UITextField * tf in self.loginView.textFiels) {
        if (tf.isFirstResponder) {
            return tf.tag;
        }
    }
    //返回一1没有当前响应者
    return 0;
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
